<!doctype html>
<html>
<head>
<meta charset="utf-8">
	<title>我的订单</title>
	<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">     
	<meta content="yes" name="apple-mobile-web-app-capable">     
	<meta content="black" name="apple-mobile-web-app-status-bar-style">     
	<meta content="telephone=no" name="format-detection">
	<link rel="shortcut icon" href="/favicon.ico">
	<link rel="stylesheet" type="text/css" href="/H5/css/sm.min.css?v=10" />
	<link rel="stylesheet" type="text/css" href="/H5/css/reset.css?v=10" />
	<link rel="stylesheet" type="text/css" href="/H5/css/order.css?v=11" />
</head>
<body>
	<div class="page-group ms-controller" :controller="cart">
        <div class="page page-current">
            <header class="bar bar-nav">
              	<a class="icon icon-left pull-left" href="javascript:window.history.go(-1)"></a>
              	<a class="icon icon-home pull-right" href="/H5"></a>
              	<h1 class="title">填写订单</h1>
            </header>
            <div class="content visibility">
	            <div :visible="@address" class="address">
	                <a :attr="{'href': '/H5/addresslist?id='+@params.id}" class="address_a">
	                	<dl>
	                    	<dt>{{@address.name||""+" "+@address.mobile_secure||""}}</dt>
	                    	<dd>
	                        	<div>{{@address.area_label||""+" "+@address.address||""}}</div>
	                        </dd>
	                    </dl>
	                    <div class="jiantou"></div>
	                </a>
	            </div>
	            <div :visible="!@address" style="text-align: center;">
	                <a :attr="{'href': '/H5/newaddress?jumpurl='+window.location.href}" style="line-height: 60px; color: #0894ec">
	                	点击添加收货地址
	                </a>
	            </div>
	            <img src="images/shopLine.png" style="width:100%; float:left;">
	            <div class="card card_first card_first01">
	                <div class="card-content">
						<div class="card-content-inner">
	                    	<a href="javascript:;"><img :attr="{'src':@data.picture_urls[0]}"/></a>
	                        <div class="order_right">
	                        	<p>{{@data.title||""}}</p>
	                            <h5>{{@data.intro||""}}</h5>
	                            <div class="jifen" :visible="@data.give_point>0"><span style="color: #4cd964; border-color: #4cd964;">赠送积分+{{@data.give_point||0}}</span><span style="color: #4cd964; border-color: #4cd964;">贡献+{{@data.contribution}}</span></div>
	                            <div class="jifen" :visible="@data.deviation_point>0"><span>抵扣积分-{{@data.deviation_point||0}}</span><span style="color: #4cd964; border-color: #4cd964;">贡献+{{@data.contribution}}</span></div>
	                        </div>
	                    </div>
	                </div>
					<ul class="address_ul">
						<li>商品单价<span>￥{{@data.sell_price||""}}</span></li>
	                    <li>购买数量
	                    	<span>
	                        	<input :on-click="@js($event)" :css="{'background-color':@count<2?'#eee':'#fff'}" name="p" type="button" value="-" style="width:25px; height: 25px; color: #000;" />
	                        	<input name="goodnum" :attr="{'value':@count}" :on-blur="@zs" type="text" style="width:40px; color: #000; font-size: 14px; height: 25px; text-align: center;" />
	                            <input :on-click="@js($event)" name="m" type="button" value="+" style="background-color: #fff; width:25px; height: 25px; color: #000;" />
	                            <!-- <input :on-click="@mjs" :css="{'background-color':@count<@data.stock?'#eee':'#fff'}" name="m" type="button" value="+" style="width:25px; color: #000;" /> -->
	                        </span>
	                    </li>
	                    <li>运费<span>￥{{@data.postage||""}}</span></li>
	                    <li :visible="@data.give_point>0">赠送总积分<span style="color: #4cd964">+{{@data.give_point*@count||""}}</span></li>
	                    <li :visible="@data.contribution>0">总贡献<span style="color: #4cd964; border-color: #4cd964">+{{@data.contribution*@count||""}}</span></li>
	                    <li :visible="@data.deviation_point>0">账户可用积分<span style="color: #4cd964">{{@info.total_point||""}}</span></li>
	                    <li :visible="@data.deviation_point>0">总抵扣积分<span>-{{@data.deviation_point*@count>@info.total_point?@info.total_point:@data.deviation_point*@count||""}}</span></li>
	                    <li>商品总价<span>￥{{@data.sell_price*@count+(@data.postage-0)||""}}</span></li>
	                    <li>
	                    	邀请代付
	                        <span>
	                            <div class="item-input">
	                              <label class="label-switch">
	                                <input name="yqPay" :on-change="@change($event)" type="checkbox">
	                                <div class="checkbox"></div>
	                              </label>
	                            </div>
	                        </span>
	                    </li>
	                    <li :visible="show">
	                    	手机号码
	                        <span>
	                            <input type="text" class="input_t" :duplex="phone" placeholder="请输入对方手机号" />
	                        </span>
	                    </li>
	                    <li :if="show">	
	                    	真实姓名
	                        <span>
	                            <input type="text" class="input_t" :duplex="name" placeholder="请输入对方真实姓名" />
	                        </span>
	                    </li>
                    </ul>
					<div class="yes">
						支付金额:<b style="color:red;">￥<span style="font-size:1.4em;">{{@ye()||""}}</span></b>
		                <a href="javascript:;" id="submit" :on-click="@submit">提交订单</a>
		            </div> 
				</div> 
            </div>
        </div>
    </div>
	<script type='text/javascript' src='/H5/js/zepto.min.js' charset='utf-8'></script>
	<script type='text/javascript' src='/H5/js/sm.min.js' charset='utf-8'></script>
	<script type='text/javascript' src='/H5/js/avalon.js' charset='utf-8'></script>
	<script type='text/javascript' src='/H5/js/simpleStorage.js' charset='utf-8'></script>
	<script type='text/javascript' src='/H5/js/mall.js' charset='utf-8'></script>
	<script>
		var cart = avalon.define({
		    $id: "cart",
		    data: {picture_urls:[]},
		    info: {},
		    address: {},
		    params: Mall.parseUrl( window.location.href ),
		    count: 1,
	    	phone: "",
	    	name: "",
	    	show: false,
	        change: function( e ){
	    		if( e.target.checked ){
	    			cart.show = true;
	    			$('.content').scrollTop( $('.content').height() );
	    		}
	    		else{
					cart.show = false;
	    		}
	        },
	        zs: function( e ){
	        	cart.count = $( e.target ).val().replace(/\D/g,"");
	        	if( !cart.count || cart.count < 1 ){
	        		cart.count = 1
	        	}
	        },
		    getAddress: function(){
		    	Mall.request( "/api/AddressList" )
		    		.always( function( data ){
		    			if( data.error_code == 0 ){
		    				if( cart.params.addressId ){
		    					for( var i = 0; i < data.data.length; i ++ ){
		    						if( data.data[i].id == cart.params.addressId  ){
		    							cart.address = data.data[i];
		    							break;
		    						}
		    					}
		    				}
		    				else{
		    					for(  var i = 0; i < data.data.length; i ++ ){
		    						if( data.data[i].is_default == 1  ){
		    							cart.address = data.data[i];
		    							break;
		    						}
		    					}
		    				}
	    					if( !cart.address.id ){
	    						cart.address = data.data[0]
	    					}
		    				$( ".content" ).removeClass( "visibility" );
		    			}
		    			else{
		    				$.alert(data.error_msg)
		    			}
		    		} )
		    },
		    getDetail: function(){
		    	Mall.request( "/api/GoodsDetail", {goods_id: cart.params.id} )
		    		.always( function( data ){
		    			if( data.error_code == 0 ){
		    				cart.data = data.data;
		    			}
		    			else{
		    				$.alert(data.error_msg)
		    			}
		    		} )
		    },
		    ye: function(){
		    	var m;
		    	if( cart.data.give_point > 0 ){
		    		return cart.data.sell_price*cart.count + ( cart.data.postage - 0 )
		    	}
		    	else{
		    		if( cart.info.total_point > 0 ){
		    			if( cart.info.total_point > cart.data.deviation_point*cart.count ){
		    				return cart.data.sell_price*cart.count - cart.data.deviation_point*cart.count + ( cart.data.postage - 0 )
		    			}
		    			else{
		    				return cart.data.sell_price*cart.count - cart.info.total_point + ( cart.data.postage - 0 )
		    			}
		    		}
		    		else{
		    			return cart.data.sell_price*cart.count + ( cart.data.postage - 0 )
		    		}
		    	}
		    },
		    js: function( e ){
		    	if($(e.target).val()=="+"){
		    		cart.count ++; 
		    	}
		    	if( $(e.target).val()=="-" ){
		    		cart.count > 1 && cart.count --;
		    	}
		    },
		    getInfo: function(){
		    	Mall.request( "/api/UserInfo" )
		    		.always( function( data ){
		    			if( data.error_code == 0 ){
		    				cart.info = data.data;
		    			}
		    			else{
		    				$.alert(data.error_msg)
		    			}
		    		} )
		    },
		    submit: function(){
		    	if( Mall.LOCK ){
		    		return
		    	}
		    	if( !cart.address ){
		    		$.toast( "请填写收货地址", 2000, "warning" )
		    		return
		    	}
    	    	if( $( ".label-switch input:checked" ).attr( "name" ) != "yqPay" ){
    		    	var postData = {
    		    		goods_id: cart.params.id,
    		    		buy_num: cart.count,
    		    		address_id: cart.address.id
    		    	}
    		    	var url = "/api/OrderSubmitBalance";
    		    	Mall.LOCK = true;
    		    	$( "#submit" ).text( "提交中" );	
    		    	Mall.request( url, postData )
    		    		.always( function( data ){
    		    			if( data.error_code == 0 ){
    		    				window.location.href="/H5/pay?orderId="+data.data.order_no;
    		    			}
    		    			else{
    		    				$.alert(data.error_msg);
    		    				$( "#submit" ).text( "提交订单" );
    		    				Mall.LOCK = false;
    		    			}
    		    		} )
    	    	}
    	    	else{
    	    		if( !Mall.mobile(cart.phone) ){
    	    			$.toast( "请输入正确手机号码", 2000, "warning" )
    	    			return
    	    		}
    	    		if( !cart.name ){
    	    			$.toast( "请输入姓名", 2000, "warning" )
    	    			return
    	    		}
    	    		var postData = {
    	    			goods_id: cart.params.id,
    	    			buy_num: cart.count,
    	    			address_id: cart.address.id,
    	    			mobile: cart.phone,
    	    			real_name: cart.name
    	    		}
    	    		Mall.LOCK = true;
    	    		$( "#submit" ).text( "提交中" );	
    	    		var url = "/api/OrderSubmitInvite";
    	    		$.prompt("请输入支付密码", function( value ){
    	    			postData.pay_password = value;
    	    			Mall.request( url, postData )
    	    				.always( function( data ){
    	    					if( data.error_code == 0 ){
    	    						$.toast( "提交代付成功", 1000, "success", function(){
    	    							window.location.href="/H5/friendlist?type=2";
    	    						} )
    	    					}
    	    					else{
    	    						$.alert(data.error_msg);
    	    						Mall.LOCK = false;
    	    					}
    	    				} )
    	    		})
    	    		$( "input.modal-text-input" ).attr( "type", "password" )
    	    	}
		    }
		})
		cart.getInfo();
		cart.getDetail();
		cart.getAddress();
		avalon.scan( document.body );
	</script>
</body>
</html>